package com.huawei.hianalytics.c;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Build;
import android.os.Build.VERSION;
import android.provider.Settings.Secure;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.AndroidRuntimeException;
import android.util.Pair;
import com.huawei.hianalytics.g.b;
import com.huawei.hianalytics.util.f;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.DnsConfig;
import com.huawei.hwCloudJs.d.d;
import com.huawei.hwid.core.constants.HwAccountConstants;
import java.lang.reflect.InvocationTargetException;

public abstract class c {
    public static String a() {
        String str;
        String str2 = "";
        try {
            str = (String) Class.forName("com.huawei.android.os.BuildEx").getMethod("getUDID", new Class[0]).invoke(null, new Object[0]);
            try {
                b.b("HianalyticsSDK", "getUDID success");
            } catch (ClassNotFoundException e) {
                b.c("HianalyticsSDK", "getUDID method invoke failed");
                return str;
            } catch (AndroidRuntimeException e2) {
                b.c("HianalyticsSDK", "getUDID getudid failed, RuntimeException is AndroidRuntimeException");
                return str;
            } catch (NoSuchMethodException e3) {
                b.c("HianalyticsSDK", "getUDID method invoke failed : NoSuchMethodException");
                return str;
            } catch (IllegalAccessException e4) {
                b.c("HianalyticsSDK", "getUDID method invoke failed : Illegal AccessException");
                return str;
            } catch (IllegalArgumentException e5) {
                b.c("HianalyticsSDK", "getUDID method invoke failed : Illegal ArgumentException");
                return str;
            } catch (InvocationTargetException e6) {
                b.c("HianalyticsSDK", "getUDID method invoke failed : InvocationTargetException");
                return str;
            }
        } catch (ClassNotFoundException e7) {
            str = str2;
            b.c("HianalyticsSDK", "getUDID method invoke failed");
            return str;
        } catch (AndroidRuntimeException e8) {
            str = str2;
            b.c("HianalyticsSDK", "getUDID getudid failed, RuntimeException is AndroidRuntimeException");
            return str;
        } catch (NoSuchMethodException e9) {
            str = str2;
            b.c("HianalyticsSDK", "getUDID method invoke failed : NoSuchMethodException");
            return str;
        } catch (IllegalAccessException e10) {
            str = str2;
            b.c("HianalyticsSDK", "getUDID method invoke failed : Illegal AccessException");
            return str;
        } catch (IllegalArgumentException e11) {
            str = str2;
            b.c("HianalyticsSDK", "getUDID method invoke failed : Illegal ArgumentException");
            return str;
        } catch (InvocationTargetException e12) {
            str = str2;
            b.c("HianalyticsSDK", "getUDID method invoke failed : InvocationTargetException");
            return str;
        }
        return str;
    }

    public static String a(Context context) {
        return context == null ? "" : context.getPackageName();
    }

    public static String b() {
        return f.b("ro.build.version.emui", "");
    }

    public static String b(Context context) {
        String str = "";
        if (context != null) {
            try {
                str = context.getPackageManager().getPackageInfo(a(context), 0).versionName;
            } catch (NameNotFoundException e) {
                b.c("HianalyticsSDK", "getVersion(): The package name is not correct!");
            }
        }
        return str;
    }

    @SuppressLint({"HardwareIds"})
    public static String c(Context context) {
        String str = "";
        if (!f.a(context, "android.permission.READ_PHONE_STATE")) {
            return str;
        }
        String deviceId;
        try {
            TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService("phone");
            if (telephonyManager != null) {
                deviceId = telephonyManager.getDeviceId();
                return deviceId;
            }
        } catch (SecurityException e) {
            b.d("HianalyticsSDK", "getDeviceID Incorrect permissions!");
        }
        deviceId = str;
        return deviceId;
    }

    @SuppressLint({"HardwareIds"})
    public static String d(Context context) {
        return context == null ? "" : Secure.getString(context.getContentResolver(), "android_id");
    }

    public static String e(Context context) {
        String str = Build.SERIAL;
        return (TextUtils.isEmpty(str) || str.equalsIgnoreCase(d.b)) ? h(context) : str;
    }

    public static String f(Context context) {
        String str = "Unknown";
        try {
            ApplicationInfo applicationInfo = context.getPackageManager().getApplicationInfo(context.getPackageName(), DnsConfig.MAX_CACHE_ENTRIES);
            if (!(applicationInfo == null || applicationInfo.metaData == null)) {
                Object obj = applicationInfo.metaData.get("CHANNEL");
                if (obj != null) {
                    String obj2 = obj.toString();
                    return obj2.length() > 256 ? "Unknown" : obj2;
                }
            }
        } catch (NameNotFoundException e) {
            b.c("HianalyticsSDK", "getChannel(): The packageName is not correct!");
        }
        return str;
    }

    public static Pair<String, String> g(Context context) {
        if (VERSION.SDK_INT > 28 || f.a(context, "android.permission.READ_PHONE_STATE")) {
            TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService("phone");
            if (telephonyManager == null) {
                return new Pair("", "");
            }
            if (telephonyManager.getSimState() != 5) {
                return new Pair("", "");
            }
            String networkOperator = telephonyManager.getNetworkOperator();
            return (TextUtils.isEmpty(networkOperator) || TextUtils.equals(networkOperator, HwAccountConstants.NULL)) ? new Pair("", "") : networkOperator.length() > 3 ? new Pair(networkOperator.substring(0, 3), networkOperator.substring(3)) : new Pair("", "");
        } else {
            b.c("HianalyticsSDK", "getMccAndMnc() Pair value is empty");
            return new Pair("", "");
        }
    }

    @TargetApi(26)
    private static String h(Context context) {
        b.a("HianalyticsSDK", "getSerial : is executed.");
        String str = "";
        if (context == null || !f.a(context, "android.permission.READ_PHONE_STATE")) {
            return str;
        }
        try {
            return Build.getSerial();
        } catch (SecurityException e) {
            b.c("HianalyticsSDK", "getSerial() Incorrect permissions!");
            return "";
        }
    }
}
